package com.beici.seoserver.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beici.seoserver.mapper.albumMapper;
import com.beici.seoserver.mapper.commentsMapper;
import com.beici.seoserver.mapper.nodeMapper;
import com.beici.seoserver.mapper.userMapper;
import com.beici.seoserver.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class nodeServiceImpl{
    @Autowired
    private nodeMapper nodeServer;
    @Autowired
    private userMapper userServer;
    @Autowired
    private albumMapper albumServer;
    @Autowired
    private commentsMapper commentsServer;


    @Transactional
    public resultes queryDetailsInfo(Long nodeId) {
        // 查出文章所有内容 取出标签、人的信息、评论的信息
        node node =  nodeServer.selectOne(new QueryWrapper<node>().eq("node_id", nodeId).select("user_id,node_time,node_id,node_tag,node_mess,node_images,node_like"));
        Integer userId = node.getUserId();
        // 查出人的信息
        user user_id = userServer.selectOne(new QueryWrapper<user>().eq("user_id", userId).select("user_id,user_name,user_mess,user_sv,user_vip,user_header"));
        userAlbumInfo userAlbum = new userAlbumInfo(
                userId,
                user_id.getUserName(),
                user_id.getUserSv(),
                user_id.getUserVip(),
                user_id.getUserMess(),
                nodeServer.selectCount(new QueryWrapper<node>().eq("user_id",user_id.getUserId())),
                user_id.getUserHeader());
        // 查出标签的信息
        String[] nodeTag = node.getNodeTag().split(",");
        ArrayList arrayList = new ArrayList();
        if (node.getNodeTag()!=null){
            for (int i = 0; i < nodeTag.length; i++) {
                album album = albumServer.selectOne(new QueryWrapper<album>().eq("ab_id", nodeTag[i]).select("ab_name,ab_id"));
                arrayList.add(album);

            }
        }
        // 查出图片
        String[] split = node.getNodeImages().split(",");
        node.setArrImgs(split);
        // 查评论
        List<comments> commens = commentsServer.selectList(new QueryWrapper<comments>().eq("node_id", nodeId).select("user_id,node_id,com_date,com_mess"));
        for (comments commen : commens) {
            user user = userServer.selectOne(new QueryWrapper<user>().eq("user_id", commen.getUserId()).select("user_name,user_header"));
            commen.setUserName(user.getUserName());
            commen.setUserHeader(user.getUserHeader());
        }
        Map map = new HashMap();
        map.put("userInfo",userAlbum);
        map.put("tag",arrayList);
        map.put("node",node);
        map.put("comments",commens);

        return resultes.success("获取成功",map);
    }
    public resultes quertyUserNodes(Integer userId) {
        List<node> user_id = nodeServer.selectUserNode(userId);
        return resultes.success("获取成功",user_id);
    }

    public resultes selectUserDetailInfo(Long ID) {
        user user_id = userServer.selectOne(new QueryWrapper<user>().eq("user_id", ID));
        return resultes.success("获取成功",user_id);
    }

    public List<node> quertyNodeAll() {
        // 查询全部文章
        List<node> user_id = nodeServer.queryChaosNode();
        // 查询文章所对应的用户
        for (node node : user_id) {
            Integer userId = node.getUserId();
            // 塞进用户信息
            user u = userServer.selectOne(new QueryWrapper<user>().eq("user_id", userId).select("user_name,user_id,user_header"));
            node.setUserName(u.getUserName());
            node.setUserHeader(u.getUserHeader());
            // 处理图片
            String[] split = node.getNodeImages().split(",");
            node.setArrImgs(split);

        }
        return user_id;
    }

    public resultes quertyDetilsTag(Long abId) {
        // 通过abId查出标签所有信息
        album tag = albumServer.selectOne(new QueryWrapper<album>().eq("ab_id", abId));
        Map info = new LinkedHashMap();
        // 查出所有帖子是否有该id
        List<node> nodes = nodeServer.selectList(new QueryWrapper<node>().select("node_id","node_look","node_tag","node_images"));
        List list = new ArrayList();
        for (node node : nodes) {
            // 拿到帖子的标签
            String nodeTag = node.getNodeTag();
            if (nodeTag.contains(String.valueOf(abId))){
                // 如果有则拿到第一张图片
                String img = node.getNodeImages().split(",")[0];
                Map map = new LinkedHashMap();
                map.put("nodeId",node.getNodeId());
                map.put("nodeImages",img);
                map.put("nodeLook",node.getNodeLook());
                list.add(map);
            }
        }
        info.put("tag",tag);
        info.put("node",list);
        return resultes.success("获取成功",info);
    }
}
